package org.cuckoos.android.command;

import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.cuckoos.android.domain.Command;
import org.cuckoos.android.domain.Constant;
import org.cuckoos.android.domain.User;
import org.cuckoos.android.ui.AppActivity;


/**
 * 
 * @author bugu
 *
 */
public class AnsentryCommand extends AbstractCommand {

	private Command command;
	
	/**
	 * 
	 * @param ds
	 * @param activity
	 * @param user
	 * @param packetId  需要应答的消息包ID
	 */
	public AnsentryCommand(DatagramSocket ds,Command c){
		super(ds);
		User me =null;
		try {
			 me = new User("cuckoos",InetAddress.getLocalHost().getHostName());
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		command = new Command(me,Constant.IPMSG_ANSENTRY);
		command.setPacketId(c.getPacketId());
		command.getUser().setIp(c.getUser().getIp());
	}
	
	public AnsentryCommand(DatagramSocket ds,AppActivity activity){
		super(ds);
		this.activity = activity;
	}
	
	@Override
	public void execute() {
		execute(command);
	}

	@Override
	public void handle(Command ac) {
		activity.updateUserList(ac.getUser());
	}

	@Override
	public boolean isHandle(Command ac) {
		return Constant.GET_MODE(ac.getCommand()) == Constant.IPMSG_ANSENTRY;
	}
	
	private AppActivity activity;
	
	public AppActivity getActivity() {
		return activity;
	}

	public void setActivity(AppActivity activity) {
		this.activity = activity;
	}

}
